<?php
// Some constant
defined('PRE_LOGIN_URL_SESSION') || define('PRE_LOGIN_URL_SESSION', 'pre_login_url_sesion_name');



// Add some share CSS & JS
Compressor::put(array(
    'css/nivo-slider.css',

    'css/jquery.dialog.css',
    'css/jquery.selectbox.css',
    'css/shared.css',
    'css/clearly.css',
    'css/message.css',
    'css/panel.css',
    'css/news.css',
    'css/pagination.css',
    'css/md/Markdown.style.css',
    'css/houses.css',
    'css/map.css',
    'css/admin.css',


    'js/jquery-1.8.3.js',
    'js/jquery.easing.1.3.js',
    'js/jquery.selectbox.js',
    'js/jquery.captcha.js',
    'js/jquery.dialog.js',
    'js/jquery.nivo.slider.js',
    'js/jquery.py.dropdown.js',
    'js/jquery.goup.js',
    'js/jquery.gmaps.js',

    'js/jquery.ext.js',
    'js/jquery.helper.js',
    'js/jquery.validate.js',
    'js/jquery.tooltip.js',
    'js/md/Markdown.Converter.js',
    'js/md/Markdown.Sanitizer.js',
    'js/md/Markdown.Editor.js',
    'js/pages/user.login.js',
    'js/pages/user.register.js',
    'js/pages/user.change.js',
    'js/pages/user.panel.js',
    'js/pages/news.post.js',
    'js/pages/house.post.js',
    'js/pages/room.js',
    'js/pages/house.manage.js',
    'js/pages/house.view.js',
    'js/pages/house.image.js',
    'js/pages/house.index.js',
    'js/pages/admin.site.js',


));

// Views
View::name('shared.message', 'message');

Route::controller(array(
    'try',
	'oauth2',
    'news',
    'user',
    'house',
    'room',
    'comment',
    'house.image',

    'admin.site',
    'admin.users',
));

Route::get('/', function()
{
    $houses = House::order_by('updated_at')->take(setting('homepage_houses'))->get();
    $title = 'Trang chủ | Tìm kiếm nhà trọ trực tuyến';
    $news = News::order_by('display_order')->order_by('updated_at')->take(setting('homepage_news'))->get();
	return View::make('home.index', compact('houses', 'title', 'news'));
});

Route::get('captcha', function() {
    return Captcha::make();
});

Route::get('tin-tuc', 'news@index');

Route::get('tin-tuc/(:any)/(:any?)', 'news@view');

Route::get('nha-tro', 'house@index');

Route::get('nha-tro/(:any)/(:any?)', 'house@view');

Route::any('get/(:any)', 'getter@index');

Route::get('tim-kiem', 'house@search');

Route::get('trang-chu', 'home@index');

Route::get('admin', 'admin.site@index');

Route::any('search', function () {
    return view('shared.search')
        ->with('title', 'Tìm kiếm');
});


Route::get('template/(:any)', function($param) {
    return View::make('shared.test.' . $param);
});

Route::any('test', function () {
    $data = Group::where_id(1)->first()->users();

    var_dump($data);
});

Route::any('dang-nhap', 'user@login');
Route::any('dang-ky', 'user@register');
Route::any('cap-nhat', 'user@update');


Event::listen('404', function()
{
	return Response::error('404');
});

Event::listen('500', function()
{
	return Response::error('500');
});

Event::listen('403', function()
{
    return Response::error('403');
});

Route::filter('before', function()
{
	// Do stuff before every request to your application...
});

Route::filter('after', function($response)
{
	// Do stuff after every request to your application...
});

Route::filter('csrf', function()
{
	if (Request::forged()) return Response::error('403');
});

Route::filter('auth', function()
{
	if (Auth::guest()) {
        pre_login_url(URI::current());
        return Redirect::to('dang-nhap');
    }
});

Route::filter('admin', function() {

    if (!MyAuth::privileges('admin')) {
        return message_page(array(
            'content' => 'Bạn không đủ quyền truy cập chức năng!',
            'title' => 'Từ chối truy cập',
            'type' => 'error'
        ));
    }
});

Route::filter('pattern: admin/*', 'admin');

